import { request } from '@umijs/max'
import { Dayjs } from 'dayjs'
import { ChannelStatisticsBO, OperateStatisticsBO, OrderStatisticsBO } from '../../types/Statistics'

export interface CustomerStatisticsVO {
  date: string
  customer_count: number
  real_price: number
  order_count: number
  already_pay: number
  goods_name: string
  seller_name: string
}

export interface CustomerStatisticsQueryDTO {
  startDay: Dayjs
  endDay: Dayjs
  dateLimit?: number
}

export const getCustomerStatistics = (params: CustomerStatisticsQueryDTO) => {
  let p = {
    ...params,
    startDay: params.startDay.format('YYYY-MM-DD'),
    endDay: params.endDay.format('YYYY-MM-DD'),
  }
  return request<CustomerStatisticsVO[]>(`/api/statistics/customerByCreator`, { params: p })
}

export const getCustomerStatisticsByChannel = (params: CustomerStatisticsQueryDTO) => {
  let p = {
    ...params,
    startDay: params.startDay.format('YYYY-MM-DD'),
    endDay: params.endDay.format('YYYY-MM-DD'),
  }
  return request<CustomerStatisticsVO[]>(`/api/statistics/getStatisticsByChannel`, { params: p })
}
export const getOrderByGoods = (params: CustomerStatisticsQueryDTO) => {
  let p = {
    ...params,
    startDay: params.startDay.format('YYYY-MM-DD'),
    endDay: params.endDay.format('YYYY-MM-DD'),
  }
  return request<CustomerStatisticsVO[]>(`/api/statistics/getOrderByGoods`, { params: p })
}
export const getRankBySeller = (params: CustomerStatisticsQueryDTO) => {
  let p = {
    ...params,
    startDay: params.startDay.format('YYYY-MM-DD'),
    endDay: params.endDay.format('YYYY-MM-DD'),
  }
  return request<CustomerStatisticsVO[]>(`/api/statistics/getRankBySeller`, { params: p })
}

export interface TargetBySellerType {
  id: string
  type: string
  user_id: string
  value: number
  unit: string
  target: number
  user_name: string
}

export const getTargetBySeller = (params: CustomerStatisticsQueryDTO) => {
  let p = {
    ...params,
    month: params.startDay.format('YYYY-MM'),
  }
  return request<TargetBySellerType[]>(`/api/statistics/getTargetBySeller`, { params: p })
}

/* ******************* 新接口 ******************* */
export const getChannelStatisticsData = async (startTime: string, endTime: string) => {
  return request<ChannelStatisticsBO[]>(`/api/v2/statistics/byChannelAndStatus`, {
    params: {
      startTime,
      endTime,
    },
  })
}

export const getOrderStatisticsData = async (startTime: string, endTime: string) => {
  return request<OrderStatisticsBO[]>(`/api/v2/statistics/byOrderAndPhotoType`, {
    params: {
      startTime,
      endTime,
    },
  })
}

export const getOperateStatisticsData = async (startTime: string, endTime: string) => {
  return request<OperateStatisticsBO[]>(`/api/v2/statistics/byOperate`, {
    params: {
      startTime,
      endTime,
    },
  })
}

export interface PhotoTypeStatisticsVO {
  field1: number
  field2: number
  field3: number
  field4: number
  field5: number
  field6: number
  field7: number
  field8: number
  userId: string
  sellerName: string
}

export interface PhotoTypeStatisticsBO extends PhotoTypeStatisticsVO {
  field1Percentage?: number
  field2Percentage?: number
  field3Percentage?: number
  field4Percentage?: number
  field5Percentage?: number
  field6Percentage?: number
  field7Percentage?: number
  field8Percentage?: number
}

export const getPhotoTypeStatisticsData = async (startTime: string, endTime: string) => {
  return request<PhotoTypeStatisticsVO[]>(`/api/v2/statistics/byPhotoType`, {
    params: {
      startTime,
      endTime,
    },
  })
}
